<?php
declare(strict_types=1);

namespace SixShop\Message\Controller\Api;

use think\Request;

/**
 * 私信API控制器
 */
class PrivateMessageController extends BaseController
{
    /**
     * 获取对话列表
     */
    public function getConversationList(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $page = $request->param('page/d', 1);
        $limit = $request->param('limit/d', 10);
        
        $result = $this->privateMessageModel->getConversationList($userId, $page, $limit);
        
        return $this->success('获取成功', $result);
    }
    
    /**
     * 获取与指定用户的对话消息
     */
    public function getConversationMessages(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $partnerId = $request->param('partner_id/d', 0);
        if ($partnerId < 0) {
            return $this->error('参数错误');
        }
        
        $page = $request->param('page/d', 1);
        $limit = $request->param('limit/d', 20);
        
        $result = $this->privateMessageModel->getConversationMessages($userId, $partnerId, $page, $limit);
        
        return $this->success('获取成功', $result);
    }
    
    /**
     * 发送私信
     */
    public function send(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $toUserId = $request->param('to_user_id/d', 0);
        $content = $request->param('content', '');
        
        if ($toUserId <= 0) {
            return $this->error('接收者不能为空');
        }
        
        if (empty($content)) {
            return $this->error('消息内容不能为空');
        }
        
        // 不能给自己发私信
        if ($toUserId == $userId) {
            return $this->error('不能给自己发送私信');
        }
        
        $result = $this->privateMessageModel->send($userId, $toUserId, $content);
        if (!$result) {
            return $this->error('发送失败，对方可能已关闭私信功能');
        }
        
        return $this->success('发送成功', ['id' => $result]);
    }
    
    /**
     * 标记私信为已读
     */
    public function markAsRead(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->privateMessageModel->markAsRead($id, $userId);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('操作成功');
    }
    
    /**
     * 标记与指定用户的所有私信为已读
     */
    public function markAllAsRead(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $partnerId = $request->param('partner_id/d', 0);
        if ($partnerId < 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->privateMessageModel->markAllAsRead($userId, $partnerId);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('操作成功');
    }
    
    /**
     * 删除私信
     */
    public function delete(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->privateMessageModel->delete($id, $userId);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 删除与指定用户的所有对话
     */
    public function deleteConversation(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $partnerId = $request->param('partner_id/d', 0);
        if ($partnerId < 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->privateMessageModel->deleteConversation($userId, $partnerId);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 获取未读私信数量
     */
    public function getUnreadCount(Request $request)
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->error('请先登录');
        }
        
        $count = $this->privateMessageModel->getUnreadCount($userId);
        
        return $this->success('获取成功', ['count' => $count]);
    }
}
